<?php
/**
 * 用户登录退出控制器
 */
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function _empty(){
        $this->redirect('Login/login');
    }
    //用户登录页面
    public function login(){
        if(IS_POST){
            $user = trim(I('user'));
            $pwd  = trim(I('pwd'));
            $res = M('admin_user')->where('name="'.$user.'"')->find();
            if(!$res){
                $arr = array(
                    'code'=>0,
                    'msg'=>L('LOGIN_ERROR_2')
                );
                $this->ajaxReturn($arr);
            }
            $res = M('admin_user')->where('name="'.$user.'" and pass="'.$pwd.'"')->find();
            if(!$res){
                $arr = array(
                    'code'=>0,
                    'msg'=>L('LOGIN_ERROR_1')
                );
                $this->ajaxReturn($arr);
            }else{
                $userid = $res['id'];
                $group= $res['group'];
                $userinfo = array(
                    'id'=>$res['id'],
                    'name'=>$res['name'],
                    'group'=>$group,
                    'login_time'=>$res['login_time']
                );
                session('user',$userinfo);
                //修改登录时间
                $user_data = array(
                    'id'=>$userid,
                    'login_time'=>time()
                );
                M('admin_user')->save($user_data);
                $arr = array(
                    'code'=>1,
                    'msg'=>L('_SUCCESS')
                );
                $this->ajaxReturn($arr);
            }
        }else{
            //判断用户是否登录
            $user = $_SESSION['user'];
            if($user){
                $this->redirect('Index/index');
            }else{
                $config = M('sys_configs')->where('id = 1')->find();
                $this->assign('system_name',$config['system_name']);
                $this->display();
            }
        }
    }
    
    //用户退出
    public function logout(){
        session(null);
        $this->success(L('_SUCCESS'),U('login'),3);
    }
}